<!DOCTYPE html>
<html>
<head>
    <meta charset="UTF-8">
    <title>修改密码</title>
    <script src="https://code.jquery.com/jquery-3.6.0.min.js"></script>
    <link rel="stylesheet" href="/layui/css/layui.css">
    <script src="/layui/layui.js"></script>
</head>
<body>
    <div class="layui-layout layui-layout-admin">
        <div class="layui-header">
            <div class="layui-logo layui-hide-xs layui-bg-black">小药店管理系统</div>
            <!-- 头部区域（可配合layui 已有的水平导航） -->
            <ul class="layui-nav layui-layout-right">
                <li class="layui-nav-item layui-hide layui-show-sm-inline-block">
                    <a>
                        <img src="//unpkg.com/outeres@0.0.10/img/layui/icon-v2.png" class="layui-nav-img">
                        欢迎, <span id="username">管理员</span>
                    </a>
                    <dl class="layui-nav-child">
                        <dd><a href="javascript:;" onclick="logout()">退出</a></dd>
                    </dl>
                </li>
            </ul>
        </div>
        <div class="layui-side layui-bg-black">
            <div class="layui-side-scroll">
                <!-- 左侧导航区域（可配合layui已有的垂直导航） -->
                <ul class="layui-nav layui-nav-tree" lay-filter="test">
                    <li class="layui-nav-item layui-nav-itemed">
                        <a class="" href="javascript:;">采购管理</a>
                        <dl class="layui-nav-child">
                            <dd><a href="../supplier/supplier.html">供应商信息管理</a></dd>
                            <dd><a href="../supplier/drug.html">药品信息管理</a></dd>
                            <dd><a href="../procurement/add.html">采购单添加</a></dd>
                            <dd><a href="../procurement/list.html">采购单管理</a></dd>
                        </dl>
                    </li>
                    <li class="layui-nav-item">
                        <a href="javascript:;">销售管理</a>
                        <dl class="layui-nav-child">
                            <dd><a href="../sales/add_xiaoshoudan.html">销售单添加</a></dd>
                            <dd><a href="../sales/xiaoshoumingxi.html">销售单管理</a></dd>
                        </dl>
                    </li>
                    <li class="layui-nav-item">
                        <a href="javascript:;">药店统计管理</a>
                        <dl class="layui-nav-child">
                            <dd><a href="../statistics/instock.html">入库统计</a></dd>
                            <dd><a href="../statistics/sales.html">销售统计</a></dd>
                            <dd><a href="../statistics/stock.html">库存查询</a></dd>
                        </dl>
                    </li>
                    <li class="layui-nav-item">
                        <a href="javascript:;">人事管理</a>
                        <dl class="layui-nav-child">
                            <dd><a href="../yuangong/add_yuangong.html">员工添加</a></dd>
                            <dd><a href="../yuangong/yuangong.html">员工管理</a></dd>
                        </dl>
                    </li>
                    <li class="layui-nav-item">
                        <a href="javascript:;">打印管理</a>
                        <dl class="layui-nav-child">
                            <dd><a href="../print/ruku.html">入库单</a></dd>
                            <dd><a href="../print/chuku.html">出库单</a></dd>
                        </dl>
                    </li>
                    <li class="layui-nav-item">
                        <a href="javascript:;">系统管理</a>
                        <dl class="layui-nav-child">
                            <dd><a href="xiuGaiMiMa.html">修改密码</a></dd>
                        </dl>
                    </li>
                </ul>
            </div>
        </div>
<div class="layui-body">
    <div class="layui-container" style="margin-top: 30px;">
        <form class="layui-form" action="">
            <div class="layui-form-item">
                <label class="layui-form-label">原密码</label>
                <div class="layui-input-block">
                    <input type="password" name="oldPassword" required lay-verify="required" 
                           placeholder="请输入原密码" autocomplete="off" class="layui-input">
                </div>
            </div>
            <div class="layui-form-item">
                <label class="layui-form-label">新密码</label>
                <div class="layui-input-block">
                    <input type="password" name="newPassword" required lay-verify="required" 
                           placeholder="请输入新密码" autocomplete="off" class="layui-input">
                </div>
            </div>
            <div class="layui-form-item">
                <label class="layui-form-label">确认密码</label>
                <div class="layui-input-block">
                    <input type="password" name="confirmPassword" required lay-verify="required" 
                           placeholder="请再次输入新密码" autocomplete="off" class="layui-input">
                </div>
            </div>
            <div class="layui-form-item">
                <div class="layui-input-block">
                    <button class="layui-btn" lay-submit lay-filter="changePwdForm">修改密码</button>
                </div>
            </div>
        </form>
    </div>
</div>
</div>
    <script>
        // 退出功能
        function logout() {
            layui.use(['jquery', 'layer'], function(){
                var $ = layui.$;
                var layer = layui.layer;
                
                $.ajax({
                    url: '/user/logout',
                    type: 'GET',
                    success: function(res) {
                        if(res.code === 0) {
                            window.location.href = '/login/login.html';
                        } else {
                            layer.msg(res.msg || '退出失败', {icon: 2});
                        }
                    },
                    error: function() {
                        layer.msg('服务器错误', {icon: 2});
                    }
                });
            });
        }

        layui.use(['form', 'layer'], function(){
            var form = layui.form;
            var layer = layui.layer;
            
            form.on('submit(changePwdForm)', function(data){
                var formData = data.field;
                
                // 验证两次输入的密码是否一致
                if(formData.newPassword !== formData.confirmPassword) {
                    layer.msg('两次输入的新密码不一致');
                    return false;
                }
                
                // 使用 jQuery 发送 AJAX 请求
                $.ajax({
                    url: '/user/changePwd',
                    type: 'POST',
                    data: {
                        oldPassword: formData.oldPassword,
                        newPassword: formData.newPassword
                    },
                    success: function(res){
                        if(res.code === 0) {
                            layer.msg('密码修改成功', {
                                icon: 1,
                                time: 1000  // 缩短提示时间
                            }, function(){
                                // 先调用登出接口
                                $.get('/user/logout', function() {
                                    // 登出后跳转到登录页
                                    window.top.location.href = '/login/login.html';
                                });
                            });
                        } else {
                            layer.msg(res.msg || '密码修改失败');
                            if(res.msg === '请先登录') {
                                setTimeout(function() {
                                    window.top.location.href = '/login/login.html';
                                }, 1000);
                            }
                        }
                    },
                    error: function(){
                        layer.msg('服务器错误，请稍后重试');
                    }
                });
                
                return false;
            });
        });
    </script>
</body>
</html> 